home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks ’92 / FinderMenu ƒ / FinderMenu App ƒ / FinderMenuApp.c next >
Encoding:
C/C++ Source or Header  |  1992-06-18  |  4.4 KB  |  203 lines  |  [TEXT/KAHL]

  1. #include "menusharing.h"
  2. #include "FinderMenuInterface.h"
  3. #include <iac.h>
  4. #include <Notification.h>
  5.  
  6. static short flexitmainloop = false; /*when true we fall thru the main event loop*/
  7.  
  8. static short theFirstMenuID;
  9.  
  10. static AEDescList thePathList;
  11. static Boolean thePathListCached;
  12. static Boolean theWorldIsGood;
  13.  
  14. static NMRec theErrorRec;
  15. static Str255 theErrorString;
  16.  
  17. #define cErrorStrings    128
  18. enum {
  19.     eNoSystem,
  20.     eNoSharedMenus,
  21.     eNoFinderMenuINIT,
  22.     eCantInstallHandlers
  23. };
  24.  
  25. static void maineventloop (void)
  26. {
  27.     EventRecord ev;
  28.  
  29.     while (! flexitmainloop) {
  30.         if (WaitNextEvent(everyEvent, &ev, 30, nil)) 
  31.             if (theWorldIsGood && ev.what == kHighLevelEvent)
  32.                 AEProcessAppleEvent(&ev);
  33.  
  34.         if (theWorldIsGood)
  35.             CheckSharedMenus(theFirstMenuID); /*See Step #2*/
  36.     } /*while*/
  37. } /*maineventloop*/
  38.  
  39.  
  40. static void initmacintosh(void) 
  41. {
  42.     /*
  43.     the magic stuff that every Macintosh application needs to do 
  44.     before doing anything else.
  45.     */
  46.  
  47.     reg short i;
  48.  
  49.     SetApplLimit(CurStackBase - 32768);
  50.  
  51.     MaxApplZone();
  52.     MoreMasters();
  53.     InitGraf(&qd.thePort);
  54. //    InitFonts();
  55. //    FlushEvents(everyEvent, 0);
  56. //    InitWindows();
  57. //    InitMenus();
  58. //    TEInit();
  59. //    InitDialogs(nil);
  60. //    InitCursor();
  61. } /*initmacintosh*/
  62.  
  63.  
  64. static pascal OSErr quitroutine(AppleEvent *event, AppleEvent *reply, long refcon)
  65. {
  66.     if (! SharedScriptCancelled(event, reply))
  67.         flexitmainloop = true;
  68.  
  69.     return (noErr);
  70. } /*quitroutine*/
  71.  
  72.  
  73. static pascal OSErr scriptcompletedcallback(event, reply, refcon)
  74.     AppleEvent *event, *reply; 
  75.     long refcon;
  76. {
  77.     AEDesc result;
  78.  
  79.     // REMINDSMZ: need to ensure ring is drawn
  80.     if (AEGetParamDesc(event, 'errs', 'TEXT', &result) == noErr) { // something went wrong!
  81.         texttostring(result.dataHandle, theErrorString);
  82.  
  83.         MemZero(&theErrorRec, sizeof(theErrorRec));
  84.         
  85.         theErrorRec.qType = nmType;
  86.         theErrorRec.nmStr = (void*) &theErrorString;
  87.         theErrorRec.nmResp = (void*) -1;    // bloody vikings
  88.         NMInstall(&theErrorRec);
  89.  
  90.         AEDisposeDesc(&result);
  91.     }
  92.  
  93.     FMFinishedProcessing();
  94.  
  95.     return noErr;
  96. }
  97.  
  98. static pascal OSErr GetFinderPaths(AppleEvent *event, AppleEvent *reply, long refcon)
  99. {
  100.     return AEPutParamDesc(reply, '----', &thePathList);
  101. }
  102.  
  103. static pascal OSErr HandleMenuHit(AppleEvent *event, AppleEvent *reply, long refcon)
  104. {
  105.     short menuID, menuItem;
  106.     DescType actualtype;
  107.     Size actualsize;
  108.  
  109.     if (thePathListCached) {    // destroy old
  110.         AEDisposeDesc(&thePathList);
  111.         thePathListCached = false;
  112.     }
  113.  
  114.     if (AEGetParamPtr(event, 'mnid', typeShortInteger, &actualtype, 
  115.         (void*)&menuID, sizeof(short), &actualsize) == noErr)
  116.         if (AEGetParamPtr(event, 'mnit', typeShortInteger, &actualtype, 
  117.             (void*)&menuItem, sizeof(short), &actualsize) == noErr)
  118.             if (AEGetKeyDesc(event, 'flis', typeAEList, &thePathList) == noErr) {
  119.                 thePathListCached = true;
  120.                 FMRollBeachball();
  121.                 SharedMenuHit(menuID, menuItem);
  122.             }
  123.  
  124.     return noErr;
  125. } /*HandleMenuHit*/
  126.  
  127. static pascal OSErr HandleCancel(AppleEvent *event, AppleEvent *reply, long refcon)
  128. {
  129.     if (MSglobals.flscriptrunning)
  130.         CancelSharedScript();
  131.  
  132.     return noErr;
  133. } /*HandleCancel*/
  134.  
  135. static pascal void NMDone(struct NMRect *aRec)
  136. {
  137.     NMRemove(aRec);
  138.     ExitToShell();
  139. }
  140.  
  141. static void BadNews(short ixErr)
  142. {
  143.     theWorldIsGood = false;
  144.     GetIndString(theErrorString, cErrorStrings, ixErr);
  145.     MemZero(&theErrorRec, sizeof(theErrorRec));
  146.     theErrorRec.qType = nmType;
  147.     theErrorRec.nmStr = (void*) &theErrorString;
  148.     theErrorRec.nmResp = (void*) NMDone;
  149.     if (NMInstall(&theErrorRec) != noErr)
  150.         ExitToShell();    // oh, well
  151. }
  152.  
  153. static void InitOtherFunctions()
  154. {
  155.     theWorldIsGood = true;    //always look on the bright side of life!
  156.     
  157. // system 7 and applevents availiable?
  158.     if (! (System7Running() || IAChaveappleevents())) {
  159.         BadNews(eNoSystem);
  160.         return;
  161.     }
  162.  
  163. // shared menus?
  164.     if (! InitSharedMenus()) {
  165.         BadNews(eNoSharedMenus);
  166.         return;
  167.     }
  168.  
  169.     MSglobals.scriptcompletedcallback = scriptcompletedcallback;
  170.  
  171. // the findermenu hack?
  172.  
  173.     theFirstMenuID = FMInit();
  174.  
  175.     if (theFirstMenuID < 0) {
  176.         BadNews(eNoFinderMenuINIT);
  177.         return;
  178.     }
  179.     
  180. // other appleevent facilities?
  181.     if (
  182.         (AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, (ProcPtr) quitroutine, 0, false) != noErr)
  183.     ||    (! IACinstallhandler(MSglobals.clientid, 'gsel', (ProcPtr) &GetFinderPaths))
  184.     ||    (! IACinstallhandler(cGetFinderMenuProc, cFinderMenuHitEvent, (ProcPtr) &HandleMenuHit))
  185.     ||    (! IACinstallhandler(cGetFinderMenuProc, cFinderCancelHitEvent, (ProcPtr) &HandleCancel))
  186.     ) {
  187.         BadNews(eCantInstallHandlers);
  188.         return;
  189.     }
  190. }
  191.  
  192. void main (void) 
  193. {
  194.     initmacintosh();
  195.  
  196.     InitOtherFunctions();
  197.  
  198.     maineventloop();
  199.  
  200.     FMRemove();
  201. } /*main*/
  202.  
  203.